import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import LassoCV
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
params = {'legend.fontsize':'x-large',
          'figure.figsize':(30,10),
          'axes.labelsize':'x-large',
          'xtick.labelsize':'x-large',
          'ytick.labelsize':'x-large',
          }
sn.set_style('whitegrid')
sn.set_context('talk')

plt.rcParams.update(params)
pd.options.display.max_colwidth = 600

from IPython.display import display, HTML
day_filename = r'E:\\images\day.csv'
train = pd.read_csv(day_filename)
train.head()

train.info()


train.describe()
#类别型特征
categorical_features = ['season','mnth','weathersit','weekday']
for col in categorical_features:
    train[col] = train[col].astype('object')

x_train_cat = train[categorical_features]
x_train_cat = pd.get_dummies(x_train_cat)
x_train_cat.head()

#数字型特征
from sklearn.preprocessing import MinMaxScaler
mn_X = MinMaxScaler()
numerical_feature = ['temp','atemp','hum','windspeed']
temp = mn_X.fit_transform(train[numerical_feature])

x_train_num = pd.DataFrame(data=temp, columns=numerical_feature,index=train.index)
x_train_num.head()

x_train =pd.concat([x_train_cat, x_train_num, train['holiday'], train['workingday']], axis=1, ignore_index=False)
x_train.head()

y = df['season']
X = df.drop(['season'], axis=1)

# 特征名称，用于后续显示权重系数对应的特征
feat_names = X.columns

# 将数据分割训练数据和测试数据


# 随机采样20%的数据构建测试样本，其余作为训练样本
X_train, X_test, y_train, y_test = train_test_split(temp, y, random_state=33, test_size=0.2)
lr = LinearRegression()

# 2.用训练数据训练模型参数
lr.fit(X_train, y_train)

# 3.用训练好的模型对测试集进行预测
y_test_pred_lr = lr.predict(X_test)
y_train_pred_lr = lr.predict(X_train)

# 4.使用r2_score评价模型在测试集和训练集上的性能，并输出评价结果

#测试集
print("The r2 score of LinearRegression on test is", r2_score(y_test, y_test_pred_lr))
#训练集
print("The r2 score of LinearRegression on train is", r2_score(y_train, y_train_pred_lr))




#岭回归

# 1.设置超参数（正则参数）范围
# alphas = [0.01, 0.1, 1, 10, 100]

# 2.生成一个RidgeCV实例
# ridge = RidgeCV(alphas=alphas, store_cv_values=True)
ridge = RidgeCV()

# 3.训练模型
ridge.fit(X_train, y_train)

# 4.预测
y_test_pred_ridge = ridge.predict(X_test)
y_train_pred_ridge = ridge.predict(X_train)

# 5.使用r2_score评价模型在测试集和训练集上的性能，并输出评价结果
# from sklearn.metrics import r2_score
#测试集
print("The r2 score of RidgeCV on test is", r2_score(y_test, y_test_pred_ridge))
#训练集
print("The r2 score of RidgeCV on train is", r2_score(y_train, y_train_pred_ridge))



#Lasso
# 1.设置超参数（正则参数）范围
# alphas = [0.01, 0.1, 1, 10, 100]

# 2.生成一个RidgeCV实例
# lasso = LassoCV(alphas=alphas)
lasso = LassoCV()

# 3.训练模型
lasso.fit(X_train, y_train)

# 4.预测
y_test_pred_lasso = lasso.predict(X_test)
y_train_pred_lasso = lasso.predict(X_train)

# 5.使用r2_score评价模型在测试集和训练集上的性能，并输出评价结果
# from sklearn.metrics import r2_score
#测试集
print("The r2 score of LassoCV on test is", r2_score(y_test, y_test_pred_lasso))
#训练集
print("The r2 score of LassoCV on train is", r2_score(y_train, y_train_pred_lasso))